from wand.image import Image
import os


def convert_jp2_to_jpg():
    orignal_path = 'H:\\古籍\\其他文件库\\美国国会图书馆\\11\\医家类'
    orignalNew_path = 'D:\\新建文件夹'

    for filepath,dirnames,filenames in os.walk(orignal_path):
        for filename in filenames:
            # print(os.path.join(orignalNew_path,filename))
            n = filepath.split('\\')
            m = n[len(n)-1]
            m2 = n[len(n) - 2]

            filePath = os.path.join(filepath, filename)
            print(filePath)
            with Image(filename=filePath) as img:
                newFile = filename.split('.')[0]
                new_ps = orignalNew_path + '\\' + m2 + '\\' + m
                if not os.path.isdir(new_ps):
                    os.makedirs(new_ps)
                fName = new_ps + '\\' + newFile+'.jpg'
                img.save(filename=fName)
            # print(fName)


    # if not os.path.isdir(orignalNew_path):
    #     os.makedirs(orignalNew_path)
    #
    # for ind, file in enumerate(os.listdir(orignal_path)):
    #     filePath = os.path.join(orignal_path, file)
    #     with Image(filename=filePath) as img:
    #         newFile = file.split('.')[0]
    #         fName = orignalNew_path + '\\' + newFile+'.jpg'
    #         img.save(filename=fName)


# with Image(filename='I:\\0001.jp2') as img:
#     img.save(filename='I:\\0010.jpg')

convert_jp2_to_jpg()
